package com.hanbang.chu_xing_gao_zhao.view_public;

import android.annotation.TargetApi;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.VideoView;

import com.hanbang.chu_xing_gao_zhao.base.BaseActivity;
import com.hanbang.chu_xing_gao_zhao.utils.MyLog;
import com.hanbang.cxgz_2.R;

/**
 * 视频播放页面
 */
public class VideoPlayActivity extends BaseActivity {
    private VideoView video;
    private MediaController mediaController;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



        setContentView(R.layout.video_play);
        initView();
        setOnClickListener();



//        Bitmap bitmap = null;
//        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
//        try {
//
//            retriever.setDataSource(url);
//            bitmap = retriever.getFrameAtTime();
//        }
//        catch(IllegalArgumentException e) {
//            e.printStackTrace();
//        }
//        catch (RuntimeException e) {
//            e.printStackTrace();
//        }



    }

    /**
     * 初始化view
     */
    private void initView() {

        video = (VideoView) findViewById(R.id.vv_video_paly);
        Intent intent = getIntent();
        url = intent.getStringExtra("url");
        playVideo(url);


    }


    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void playVideo(String url) {
        // 控制视频播放
        mediaController = new MediaController(this);
        Uri uri = Uri.parse(url);
        video.setMediaController(mediaController);
        video.setVideoURI(uri);


//        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
//                RelativeLayout.LayoutParams.MATCH_PARENT,
//                RelativeLayout.LayoutParams.MATCH_PARENT);
//        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
//        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
//        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
//        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
//        video.setLayoutParams(layoutParams);

        video.start();


        //开始播放时调用此方法
        video.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {
//                MyLog.e("text", "\tAAA**isLooping__" + mp.isLooping() + "\twhat" + what + "\textra" + extra);

                ((ProgressBar) findViewById(R.id.progressBar2)).setVisibility(View.GONE);

                return false;
            }
        });



        //播放结束时调用此方法
        video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
//                MyLog.e("text", "\tBBB**isLooping__" + mp.isLooping());
            }
        });


    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);


    }

    /**
     * 设置点击事件
     */
    private void setOnClickListener() {

    }

    /**
     * 处理监听
     */
    @SuppressWarnings("unused")
    private OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {

                default:
                    break;
            }
        }
    };


}
